/*
 * Copyright 2009-2009 Tanesha Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *    	 this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *    	 this list of conditions and the following disclaimer in the documentation 
 *    	 and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those 
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Tanesha Project.
 */
package com.google.api.chart;

import java.util.List;

import com.google.api.chart.encoding.JoinHelper;
import com.google.api.chart.features.ColorFeature;
import com.google.api.chart.features.FillFeature;
import com.google.api.chart.features.MapFeature;

/**
 * Map Chart implmentation.
 * <p>
 * Maps must be configured to show a region, and a set of locations. Combined with
 * chart data and colors, countries or states (USA) can be shown. 
 * </p>
 * <p>
 * <code><pre>
 * import static com.google.api.chart.LocationMaker.*;
 * import static com.google.api.chart.ChartMaker.*;
 * ...
 * new ChartMaker().type(map().africa()
 * 		.locations(ALGERIA, SOMALIA, EGYPT)
 * 		.colors(color("FF0000"), color("00FF00"), color("0000FF)))
 * .data(dataSeries(100, 50, 0)
 * </pre></code>
 * </p>
 * <p>
 * See {@link http://code.google.com/apis/chart/types.html#maps}
 * </p>
 * @author sorenad
 */
public final class MapChartMaker implements ChartTypeMaker, ColorFeature<MapChartMaker>, FillFeature<MapChartMaker>, MapFeature<MapChartMaker> {

	private CommonFeatureHelper<MapChartMaker> common;

	private String chtm = "world";

	private String chld;
	
	protected MapChartMaker() {
		this.common = new CommonFeatureHelper<MapChartMaker>(this);
	}
	
	/**
	 * Set map to show africa region.
	 * @return
	 */
	public MapChartMaker africa() {
		this.chtm = "africa";
		return this;
	}
	/**
	 * Set map to show asia region.
	 * @return
	 */
	public MapChartMaker asia() {
		this.chtm = "asia";
		return this;
	}
	/**
	 * Set map to show europe region.
	 * @return
	 */
	public MapChartMaker europe() {
		this.chtm = "europe";
		return this;
	}
	/**
	 * Set map to show middle east region.
	 * @return
	 */
	public MapChartMaker middleEast() {
		this.chtm = "middle_east";
		return this;
	}
	/**
	 * Set map to show south america region.
	 * @return
	 */
	public MapChartMaker southAmerica() {
		this.chtm = "south_america";
		return this;
	}
	/**
	 * Set map to show usa region.
	 * @return
	 */
	public MapChartMaker usa() {
		this.chtm = "usa";
		return this;
	}
	/**
	 * Set map to show the world.
	 * @return
	 */
	public MapChartMaker world() {
		this.chtm = "world";
		return this;
	}

	@Override
	public MapChartMaker locations(LocationMaker... locations) {
		this.chld = JoinHelper.join(locations, "");
		return this;
	}
	
	@Override
	public MapChartMaker colors(ColorMaker... colors) {
		return common.colors(colors);
	}
	
	@Override
	public MapChartMaker fills(FillMaker... fills) {
		for (FillMaker fill : fills) {
			if (!(fill.isBackground() && fill instanceof FillSolidMaker))
				throw new IllegalArgumentException("Map Charts only allows solid background fills.");
		}
		return common.fills(fills);
	}
	
	@Override
	public void fillParameters(List<String> parameters, List<String> chmParameters) {
		if (chtm != null)
			parameters.add("chtm=" + chtm);
		if (chld != null)
			parameters.add("chld=" + chld);
		
		common.fillParameters(parameters, chmParameters);
	}
	
	@Override
	public String toString() {
		return "t";
	}

}
